package com.ymatou.iapi.godmch.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.bindmchBean;
import com.ymatou.iapi.godmch.parameter.createagentBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.editTypeagentBean;
import com.ymatou.iapi.godmch.service.createagentCall;
import com.ymatou.iapi.godmch.service.editTypeagentCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.model.mch_relation;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
/**
*设置代理商类型
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_editTypeagent{
	private static editTypeagentBean bean;
	private static editTypeagentCall call;
	DefGodMchWapper DefGodMchWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("设置代理商类型");
	}

	@Before
	public void caseUp() {
		bean=new editTypeagentBean();
		call=new editTypeagentCall();
		DefGodMchWapper=new DefGodMchWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editTypeagent_001() {
		Logger.start(true,"设置代理商类型,验证商户id和代理商id同时存在-返回正常");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			List<Integer> atlist = GodmchManageCallService.getNotMyAgentType(ccbean.getMchId(), ccbean.getRoleType());
			  //获取一个AgentType 这个AgentType不是当前这个代理商在用
		    bean.setMchId(ccbean.getMchId());
            bean.setAgentId(agentid);
            bean.setAgentType(atlist.get(0));
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			mch_relation dbmr = DefGodMchWapper.selectMchRelation(ccbean.getMchId(), agentid, null).get(0);
			Logger.verifyEquals(bean.getAgentType(), dbmr.getAgentType(), "验证AgentType");
			
			//发消息总线
			MessageListBean MessageListBean = MqQueryCallService.initGodmchManage("change_agent_type-"+bean.getMchId()+"-"+bean.getAgentType(),"agent_changetype");
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(bean.getAgentType().toString(), joa.get(0).get("agentTypeId").toString(), "验证总线消息agentTypeId");
			Logger.verifyEquals(bean.getAgentId().toString(), joa.get(0).get("updatedUserId").toString(), "验证总线消息updatedUserId");
			Logger.verifyEquals(bean.getMchId().toString(), joa.get(0).get("userId").toString(), "验证总线消息userId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editTypeagent_001_1() {
		Logger.start(true,"设置代理商类型,验证商户id和代理商id同时存在-代理模式 1自主选货-返回正常");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			List<Integer> atlist = GodmchManageCallService.getNotMyAgentType(ccbean.getMchId(), ccbean.getRoleType());
			  //获取一个AgentType 这个AgentType不是当前这个代理商在用
		    bean.setMchId(ccbean.getMchId());
            bean.setAgentId(agentid);
            bean.setAgentType(atlist.get(0));
            bean.setAgentModel(1);//代理模式, 1.自主选货,2.自动继承. 为空时默认为：1
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			mch_relation dbmr = DefGodMchWapper.selectMchRelation(ccbean.getMchId(), agentid, null).get(0);
			Logger.verifyEquals(bean.getAgentType(), dbmr.getAgentType(), "验证AgentType");
			Logger.verifyEquals(bean.getAgentModel(), dbmr.getAgentModel(), "验证AgentModel");
			
//			//发消息总线
//			MessageListBean MessageListBean = MqQueryCallService.initGodmchManage("change_agent_type-"+bean.getMchId()+"-"+bean.getAgentType(),"agent_changetype");
//			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
//			if(joa.size()==0){
//				Thread.sleep(5000);
//				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
//			}
//			joa.forEach(ele->{
//				Logger.debug(ele.toString());
//			});
//			Logger.verifyEquals(bean.getAgentType().toString(), joa.get(0).get("agentTypeId").toString(), "验证总线消息agentTypeId");
//			Logger.verifyEquals(bean.getAgentId().toString(), joa.get(0).get("updatedUserId").toString(), "验证总线消息updatedUserId");
//			Logger.verifyEquals(bean.getMchId().toString(), joa.get(0).get("userId").toString(), "验证总线消息userId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editTypeagent_001_2() {
		Logger.start(true,"设置代理商类型,验证商户id和代理商id同时存在-代理模式 2.自动继承-返回正常(返回失败,20200318修改，不支持自动继承)");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			List<Integer> atlist = GodmchManageCallService.getNotMyAgentType(ccbean.getMchId(), ccbean.getRoleType());
			  //获取一个AgentType 这个AgentType不是当前这个代理商在用
		    bean.setMchId(ccbean.getMchId());
            bean.setAgentId(agentid);
            bean.setAgentType(atlist.get(0));
            bean.setAgentModel(2);//代理模式 2.自动继承
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("5000", call.getString("status"), "验证返回成功status值");
	        Logger.verifyEquals(true, call.getString("msg").contains("抱歉，平台商户不支持继承型代理"), "验证返回成功Message值");
	        
//			Logger.verifyEquals("200", call.getString("status"), "验证status");
//			mch_relation dbmr = DefGodMchWapper.selectMchRelation(ccbean.getMchId(), agentid, null).get(0);
//			Logger.verifyEquals(bean.getAgentType(), dbmr.getAgentType(), "验证AgentType");
//			Logger.verifyEquals(bean.getAgentModel(), dbmr.getAgentModel(), "验证AgentModel");
			
//			//发消息总线
//			MessageListBean MessageListBean = MqQueryCallService.initGodmchManage("change_agent_type-"+bean.getMchId()+"-"+bean.getAgentType(),"agent_changetype");
//			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
//			if(joa.size()==0){
//				Thread.sleep(5000);
//				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
//			}
//			joa.forEach(ele->{
//				Logger.debug(ele.toString());
//			});
//			Logger.verifyEquals(bean.getAgentType().toString(), joa.get(0).get("agentTypeId").toString(), "验证总线消息agentTypeId");
//			Logger.verifyEquals(bean.getAgentId().toString(), joa.get(0).get("updatedUserId").toString(), "验证总线消息updatedUserId");
//			Logger.verifyEquals(bean.getMchId().toString(), joa.get(0).get("userId").toString(), "验证总线消息userId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editTypeagent_001_3() {
		Logger.start(true,"设置代理商类型,验证商户id和代理商id关系不存在-代理模式 2.自动继承-返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			
			CreateCodeBean ccbean2 = GodmchManageCallService.CreateAllMsgSellerMch();
			String code2 = GodmchManageCallService.createCodeCall(ccbean2).getCode();
			
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			List<Integer> atlist = GodmchManageCallService.getNotMyAgentType(ccbean.getMchId(), ccbean.getRoleType());
			
			  //获取一个AgentType 这个AgentType不是当前这个代理商在用
		    bean.setMchId(ccbean2.getMchId());
            bean.setAgentId(agentid);
            bean.setAgentType(atlist.get(0));
            bean.setAgentModel(2);//代理模式 2.自动继承
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("5002", call.getString("status"), "验证status");
			Logger.verifyEquals("代理商不存在", call.getString("msg"), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editTypeagent_001_4() {
		Logger.start(true,"设置代理商类型,验证一个代理商有多个渠道商，修改代理商的代理模式 2.自动继承-返回错误(返回失败,20200318修改，不支持自动继承)");
		try {
			//a、b渠道，a代理c，b代理c，c有两个渠道，修改代理商模式 2.自动继承，返回代理模式冲突
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			
			CreateCodeBean ccbean2 = GodmchManageCallService.CreateAllMsgSellerMch();
			String code2 = GodmchManageCallService.createCodeCall(ccbean2).getCode();
			
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			List<Integer> atlist = GodmchManageCallService.getNotMyAgentType(ccbean.getMchId(), ccbean.getRoleType());
			
			//绑定
			bindmchBean bindmchBean=GodmchManageCallService.InitbindmchBean(agentid, code2);
			GodmchManageCallService.bindmchcall(bindmchBean);
			  //获取一个AgentType 这个AgentType不是当前这个代理商在用
		    bean.setMchId(ccbean.getMchId());
            bean.setAgentId(agentid);
            bean.setAgentType(atlist.get(0));
            bean.setAgentModel(2);//代理模式 2.自动继承
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("5000", call.getString("status"), "验证返回成功status值");
	        Logger.verifyEquals(true, call.getString("msg").contains("抱歉，平台商户不支持继承型代理"), "验证返回成功Message值");
	        
			/*Logger.verifyEquals("5000", call.getString("status"), "验证status");
			Logger.verifyEquals(true, call.getString("msg")
					.contains("抱歉，您已有代理身份，无法再成为自动继承型代理，请联系您的渠道商切换代理模式！"), "验证msg");*/
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editTypeagent_002() {
		Logger.start(true,"设置代理商类型,验证商户id与代理商id不匹配-返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			CreateCodeBean ccbean2 = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			List<Integer> atlist = GodmchManageCallService.getNotMyAgentType(ccbean.getMchId(), ccbean.getRoleType());
			  //获取一个AgentType 这个AgentType不是当前这个代理商在用
		    bean.setMchId(ccbean2.getMchId());
            bean.setAgentId(agentid);
            bean.setAgentType(atlist.get(0));
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("5002", call.getString("status"), "验证status");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editTypeagent_003() {
		Logger.start(true,"设置代理商类型,验证商户id不存在,返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			List<Integer> atlist = GodmchManageCallService.getNotMyAgentType(ccbean.getMchId(), ccbean.getRoleType());
			  //获取一个AgentType 这个AgentType不是当前这个代理商在用
		    bean.setMchId(-1);
            bean.setAgentId(agentid);
            bean.setAgentType(atlist.get(0));
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("5002", call.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editTypeagent_004() {
		Logger.start(true,"设置代理商类型,验证商户id为空,返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			List<Integer> atlist = GodmchManageCallService.getNotMyAgentType(ccbean.getMchId(), ccbean.getRoleType());
			  //获取一个AgentType 这个AgentType不是当前这个代理商在用
//		    bean.setMchId(ccbean.getMchId());
            bean.setAgentId(agentid);
            bean.setAgentType(atlist.get(0));
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("4001", call.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editTypeagent_005() {
		Logger.start(true,"设置代理商类型,验证代理商id不存在,返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			List<Integer> atlist = GodmchManageCallService.getNotMyAgentType(ccbean.getMchId(), ccbean.getRoleType());
			  //获取一个AgentType 这个AgentType不是当前这个代理商在用
		    bean.setMchId(ccbean.getMchId());
            bean.setAgentId(-1);
            bean.setAgentType(atlist.get(0));
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("5002", call.getString("status"), "验证status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editTypeagent_006() {
		Logger.start(true,"设置代理商类型,验证代理商id为空,返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			List<Integer> atlist = GodmchManageCallService.getNotMyAgentType(ccbean.getMchId(), ccbean.getRoleType());
			  //获取一个AgentType 这个AgentType不是当前这个代理商在用
		    bean.setMchId(ccbean.getMchId());
//            bean.setAgentId(agentid);
            bean.setAgentType(atlist.get(0));
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("4001", call.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editTypeagent_007() {
		Logger.start(true,"设置代理商类型,验证代理商类型id为空,返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			List<Integer> atlist = GodmchManageCallService.getNotMyAgentType(ccbean.getMchId(), ccbean.getRoleType());
			  //获取一个AgentType 这个AgentType不是当前这个代理商在用
		    bean.setMchId(ccbean.getMchId());
            bean.setAgentId(agentid);
//            bean.setAgentType(atlist.get(0));
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("4001", call.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editTypeagent_008() {
		Logger.start(true,"重复设置代理商类型-不发消息总线,需要优化");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			List<Integer> atlist = GodmchManageCallService.getNotMyAgentType(ccbean.getMchId(), ccbean.getRoleType());
			  //获取一个AgentType 这个AgentType不是当前这个代理商在用
		    bean.setMchId(ccbean.getMchId());
            bean.setAgentId(agentid);
            bean.setAgentType(atlist.get(0));
			call.setData(bean);
			call.callService();
			call.callService();
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			mch_relation dbmr = DefGodMchWapper.selectMchRelation(ccbean.getMchId(), agentid, null).get(0);
			Logger.verifyEquals(bean.getAgentType(), dbmr.getAgentType(), "验证AgentType");
			
//			//发消息总线
//			MessageListBean MessageListBean = MqQueryCallService.initGodmchManage("change_agent_type-"+bean.getMchId()+"-"+bean.getAgentType(),"agent_changetype");
//			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
//			if(joa.size()==0){
//				Thread.sleep(5000);
//				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
//			}
//			joa.forEach(ele->{
//				Logger.debug(ele.toString());
//			});
//			Logger.verifyEquals(true,joa.isEmpty(), "验证总线消息agentTypeId 没有更改不应该发消息总显得，需要优化");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editTypeagent_009() {
		Logger.start(true,"设置代理商类型,AgentType不是自己的类型失败");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			CreateCodeBean ccbean2 = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			  //获取一个AgentType 这个AgentType不是当前这个代理商在用
		    bean.setMchId(ccbean.getMchId());
            bean.setAgentId(agentid);
            bean.setAgentType(ccbean2.getRoleType());
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("5001", call.getString("status"), "验证status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editTypeagent_010() {
		Logger.start(true,"设置代理商类型,验证商户id和代理商id同时存在-代理模式 1自主选货-返回正常");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			List<Integer> atlist = GodmchManageCallService.getNotMyAgentType(ccbean.getMchId(), ccbean.getRoleType());
			  //获取一个AgentType 这个AgentType不是当前这个代理商在用
		    bean.setMchId(ccbean.getMchId());
            bean.setAgentId(agentid);
            bean.setAgentType(atlist.get(0));
            bean.setAgentModel(1);//代理模式, 1.自主选货,2.自动继承. 为空时默认为：1
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			mch_relation dbmr = DefGodMchWapper.selectMchRelation(ccbean.getMchId(), agentid, null).get(0);
			Logger.verifyEquals(bean.getAgentType(), dbmr.getAgentType(), "验证AgentType");
			Logger.verifyEquals(bean.getAgentModel(), dbmr.getAgentModel(), "验证AgentModel");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editTypeagent_012() {
		Logger.start(true,"设置代理商类型,验证不在相同代理模式的代理类型中切换-返回失败");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			
            createagentBean createagentBean= GodmchManageCallService.InitCreateAgentTypeBean(ccbean.getMchId());
            createagentBean.setAgentMode(2);
            createagentBean.setRebateRate(new BigDecimal(2));
            GodmchManageCallService.CreateAgentTypeCall(createagentBean);
            
			List<Integer> atlist = GodmchManageCallService.getNotMyAgentType(ccbean.getMchId(), ccbean.getRoleType());
			  //获取一个AgentType 这个AgentType不是当前这个代理商在用
		    bean.setMchId(ccbean.getMchId());
            bean.setAgentId(agentid);
            bean.setAgentType(atlist.get(1));
            bean.setAgentModel(1);
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("5000", call.getString("status"), "验证status");
			Logger.verifyEquals(true, call.getString("msg").contains("代理类型的代理模式不一致，禁止切换"), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}